<template>
  <div v-loading.lock="loading" class="base-content">
    <div class="basic-info">
      <el-form ref="queryForm" :model="queryForm" inline>
        <el-form-item
          :label="sysBusinessSampleCategory.name"
          prop="sampleCategoryName"
        >
          <el-input
            v-model="queryForm.sampleCategoryName"
            :placeholder="sysBusinessSampleCategory.name"
            clearable
            @keyup.enter.native="getList()"
          />
        </el-form-item>
        <el-form-item class="base-filter-button">
          <el-button
            v-loading="searchLoading"
            type="primary"
            @click="onQuery"
            >{{ operateType.query }}</el-button
          >
          <el-button @click="cancelForm">{{ operateType.clear }}</el-button>
        </el-form-item>
      </el-form>

      <!--操作栏-->
      <div class="base-content-operate">
        <el-button
          v-rights="'Add'"
          icon="el-icon-plus"
          type="primary"
          @click="onNew"
          >{{ operateType.add }}</el-button
        >

        <el-button
          v-rights="'Import'"
          icon="el-icon-document-copy"
          type="primary"
          @click="importData"
          >{{ operateType.import }}</el-button
        >

        <!-- <el-button
          icon="el-icon-document-copy"
          type="primary"
          @click="importData"
          >导出</el-button> -->
      </div>

      <el-table
        :data="list"
        element-loading-text="Loading"
        fit
        highlight-current-row
      >
        <el-table-column
          align="center"
          :label="sysBusinessSampleCategory.serialNumber"
          width="50"
        >
          <template slot-scope="scope">
            {{ scope.$index + 1 }}
          </template>
        </el-table-column>
        <el-table-column :label="sysBusinessSampleCategory.sampleCategoryID">
          <template slot-scope="scope">
            {{ scope.row.sampleCategoryNum }}
          </template>
        </el-table-column>
        <el-table-column :label="sysBusinessSampleCategory.name">
          <template slot-scope="scope">
            {{ scope.row.sampleCategoryName }}
          </template>
        </el-table-column>
        <el-table-column :label="sysBusinessSampleCategory.alias">
          <template slot-scope="scope">
            {{ scope.row.anotherName }}
          </template>
        </el-table-column>
        <el-table-column :label="sysBusinessSampleCategory.code">
          <template slot-scope="scope">
            {{ scope.row.sampleCategoryCode }}
          </template>
        </el-table-column>
        <el-table-column :label="sysBusinessSampleCategory.sampleForm">
          <template slot-scope="scope">
            {{ scope.row.shape }}
          </template>
        </el-table-column>
        <el-table-column :label="sysBusinessSampleCategory.singleCapacity">
          <template slot-scope="scope">
            {{ scope.row.capacity }}
          </template>
        </el-table-column>
        <el-table-column :label="sysBusinessSampleCategory.liter">
          <template slot-scope="scope">
            {{ scope.row.capacityUnit }}
          </template>
        </el-table-column>
        <el-table-column :label="sysBusinessSampleCategory.countingUnit">
          <template slot-scope="scope">
            {{ scope.row.countingUnit }}
          </template>
        </el-table-column>
        <el-table-column :label="sysBusinessSampleCategory.inspectionBasis">
          <template slot-scope="scope">
            {{ scope.row.inspectionBasis }}
          </template>
        </el-table-column>
        <el-table-column :label="operateType.action">
          <template slot-scope="scope">
            <div class="base-table-operation">
              <el-link
                size="mini"
                type="primary"
                @click="handleEdit(scope.row)"
                >{{ operateType.redact }}</el-link
              >
              <el-link
                size="mini"
                type="danger"
                @click="handleDelete(scope.row)"
                >{{ operateType.delete }}</el-link
              >
            </div>
          </template>
        </el-table-column>
      </el-table>

      <BasePagination
        :total="pagination.total"
        :page-index="pagination.pageIndex"
        :page-size="pagination.pageSize"
        @sizeChange="sizeChange"
        @changePage="currentChange"
      />
    </div>
    <BusinessImportData
      v-if="dialogVisible"
      ref="BusinessImportData"
      :dialog-visible.sync="dialogVisible"
      :sys-business-import-type.sync="sysBusinessImportType"
      @load="getList"
    />

    <SysBusinessSampleCategoryEdit
      ref="SysBusinessSampleCategoryEdit"
      :dialog-form-visible.sync="dialogFormVisible"
      :edit-id.sync="editId"
    />
  </div>
</template>

<script>
import {
  deleteRecord,
  getList,
  getSampleTree,
} from "../../../../api/DetectionItemManage/SysBusinessSampleCategory.js";
import BusinessImportData from "../../../../components/SysBusinessImportDialog.vue";
import SysBusinessSampleCategoryEdit from "./SysBusinessSampleCategoryEdit.vue";
import { formatData } from "../../../../utils/index";
export default {
  name: "SysBusinessSampleCategoryList",
  components: { SysBusinessSampleCategoryEdit, BusinessImportData },
  data() {
    return {
      sysBusinessImportType: "1",
      dialogVisible: false,
      loading: true,
      list: [],
      editId: "",
      queryForm: {
        parentId: 0,
        sampleCategoryName: "",
        sampleCategoryNum: "",
        anotherName: "",
        sampleCategoryCode: "",
        shape: "",
        capacity: "",
        capacityUnit: "",
        countingUnit: "",
        level: 0,
      },
      dialogFormVisible: false,
      pagination: {
        pageIndex: 1,
        pageSize: 10,
        total: 0,
      },
      searchLoading: false,
    };
  },
  computed: {
    operateType() {
      return this.$t("ModuleSysBusiness.OperateType") || {};
    },
    sysBusinessSampleCategory() {
      return this.$t("SysBusinessSampleCategory") || {};
    },
  },
  created() {
    this.getList();
  },
  methods: {
    // 导入
    importData() {
      this.dialogVisible = true;
    },
    // 获取样品树
    getSampleTreeData() {
      getSampleTree().then((res) => {
        const formatMenuData = formatData(
          [
            {
              id: "0",
              label: this.$t("SysBusinessSampleCategory.firstLevelMenu"),
              children: res.data,
            },
          ],
          { value: "id", label: "label" }
        );
        this.$refs.SysBusinessSampleCategoryEdit.sampleTreeData =
          formatMenuData;
      });
    },
    currentChange(val) {
      this.pagination.pageIndex = val;
      this.getList();
    },
    sizeChange(val) {
      this.pagination.pageIndex = 1;
      this.pagination.pageSize = val;
      this.getList();
    },
    getList() {
      this.loading = true;
      getList(this.queryForm, this.pagination)
        .then((res) => {
          this.list = res.data.dataList ? res.data.dataList : [];
          this.pagination.total = res.data.total;
          this.loading = false;
        })
        .catch(() => {
          this.loading = false;
        });
      this.getSampleTreeData();
    },
    onNew() {
      this.$refs.SysBusinessSampleCategoryEdit.formData = this.Helper.deepCopy(
        this.$refs.SysBusinessSampleCategoryEdit.initData
      );
      this.dialogFormVisible = true;
    },
    handleDelete(rowData) {
      this.Helper.deleteConfirm(() => {
        this.loading = true;
        deleteRecord({ id: rowData.id })
          .then((res) => {
            this.getList();
            this.loading = false;
          })
          .catch(() => {
            this.loading = false;
          });
      });
    },
    handleEdit(rowData) {
      this.editId = rowData.id;
      this.dialogFormVisible = true;
    },
    onQuery() {
      this.getList();
    },
    // 清空筛选条件
    cancelForm() {
      this.$refs.queryForm.resetFields();
      this.getList();
    },
  },
};
</script>
